package com.wanwucs.timelylibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by chenhaitao on 2017/8/3.
 */

public class TimelyTimePickerView extends View {

    private Paint mPaint;

    private int mHourextSize,mLineWidth,mRectHeight,mRectTextSize;

    private int[] mHours = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};

    private float mTouchHeight;

    private static final float SCALE = 1.25f;

    private int mRoundHour;

    private int mLeftLineColor,mRectTextColor;

    public TimelyTimePickerView(Context context) {
        this(context,null);
    }

    public TimelyTimePickerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimelyTimePickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Timely);
        mHourextSize = typedArray.getDimensionPixelSize(R.styleable.Timely_hourTextSize,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
        mLineWidth = typedArray.getDimensionPixelSize(R.styleable.Timely_leftLineWidth,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()));
        mLeftLineColor = typedArray.getColor(R.styleable.Timely_leftLineColor,Color.BLACK);
        mRectTextSize = typedArray.getDimensionPixelSize(R.styleable.Timely_rectTextSize,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 30, getResources().getDisplayMetrics()));
        mRectTextColor = typedArray.getColor(R.styleable.Timely_rectTextColor,Color.RED);
        typedArray.recycle();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawRectAndTime(canvas,mRoundHour+":00");
        for (int i=0;i<mHours.length;i++){
            int hour = mHours[i];
            int sub = Math.abs(hour -mRoundHour);
            float textHeight = getAveHeight()*(hour+1);
            if (sub==0){
                drawLine(canvas,getAveHeight()*(hour+1),mLineWidth,SCALE*SCALE*SCALE*SCALE);
                drawText(canvas,i+"",textHeight,SCALE*SCALE*SCALE*SCALE);
            }else if (sub==1){
                drawLine(canvas,getAveHeight()*(hour+1),mLineWidth,SCALE*SCALE*SCALE);
                drawText(canvas,i+"",textHeight,SCALE*SCALE*SCALE);
            }else if (sub==2){
                drawLine(canvas,getAveHeight()*(hour+1),mLineWidth,SCALE*SCALE);
                drawText(canvas,i+"",textHeight,SCALE*SCALE);
            }else if (sub==3){
                drawLine(canvas,getAveHeight()*(hour+1),mLineWidth,SCALE);
                drawText(canvas,i+"",textHeight,SCALE);
            }else {
                drawLine(canvas,getAveHeight()*(hour+1),mLineWidth,1);
                drawText(canvas,i+"",textHeight,1);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                mTouchHeight = event.getY();
                mRoundHour = getRoundHour(mTouchHeight);
                Log.e("mRoundHour",mRoundHour+"");
                invalidate();
                break;
        }
        return true;
    }
    /*
    * 画左边的线
    * @param canvas  画布
    * @param startY  画线的Y轴坐标
    * @param stopX   画线的长度
    * @param scale   缩放比例
    *
    * */
    private void drawLine(Canvas canvas,float startY,float stopX,float scale){
        mPaint.reset();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(2.0f);
        mPaint.setColor(mLeftLineColor);
        float f1 = 1/scale;
        canvas.scale(scale,scale);
        canvas.drawLine(0,startY*f1,stopX,startY*f1,mPaint);
        canvas.scale(f1,f1);
    }

    /*
    * 画线右边的文字
    * @param canvas  画布
    * @param text    文字
    * @param stopX   文字base高度
    * @param scale   缩放比例
    *
    * */
    private void drawText(Canvas canvas,String text,float height,float scale){
        mPaint.reset();
        mPaint.setTextSize(mHourextSize);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        float f1 = 1/scale;
        canvas.scale(scale,scale);
        canvas.drawText(text,mLineWidth,(height+14)*f1,mPaint);
        canvas.scale(f1,f1);
    }

    /*
    * 获取触摸点的小时数
    * @param touchHeight  触摸点的Y坐标
    *
    * */
    private int getRoundHour(float touchHeight){
        float roundHour = touchHeight/(getHeight()/(mHours.length+1))-1;
        int hour = Math.round(roundHour);
        if (hour<0){
            return 0;
        }
        if (hour>24){
            return 24;
        }
        return hour;
    }

    /*
    * 获取平均线的高度
    *
    * */
    private float getAveHeight(){
        return getHeight()/(mHours.length+1);
    }

    /*
    * 画矩形框和里面的时间
    * @param canvas  画布
    * @param time    时间
    * */
    private void  drawRectAndTime(Canvas canvas,String time){
        RectF rectF = null;
        mRectHeight = getHeight()/(mHours.length+1)*2;
        if (mTouchHeight-mRectHeight/2<0){
            rectF = new RectF(0,0,getWidth(),mRectHeight);
        }else if (mTouchHeight > mRectHeight/2){
            rectF = new RectF(0,mTouchHeight-mRectHeight/2,getWidth(),mTouchHeight+mRectHeight/2);
        }else if (getHeight()-mTouchHeight>mRectHeight/2){
            rectF = new RectF(0,getHeight()-mRectHeight,getWidth(),getHeight());
        }
        mPaint.setColor(Color.GRAY);
        mPaint.setAlpha(80);
        canvas.drawRect(rectF,mPaint);

        mPaint.reset();
        mPaint.setColor(mRectTextColor);
        mPaint.setTextSize(mRectTextSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        float baseline = (rectF.bottom + rectF.top - fontMetrics.bottom - fontMetrics.top) / 2;
        canvas.drawText(time, rectF.centerX(), baseline, mPaint);
    }

}
